 package server;

 import client.CardData;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import constants.SkillConstants;
 import database.DatabaseConnection;
 import java.io.File;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import provider.MapleData;
 import provider.MapleDataProvider;
 import provider.MapleDataProviderFactory;
 import provider.MapleDataTool;
 import tools.Pair;
 import tools.Triple;







 public class CharacterCardFactory
 {
/*  34 */   private static final Logger log = LogManager.getLogger();
/*  35 */   private static final CharacterCardFactory instance = new CharacterCardFactory();
/*  36 */   protected final MapleDataProvider etcData = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Etc.wz"));
/*  37 */   protected final Map<Integer, Integer> cardEffects = new HashMap<>();
/*  38 */   protected final Map<Integer, List<Integer>> uniqueEffects = new HashMap<>();

   public static CharacterCardFactory getInstance() {
/*  41 */     return instance;
   }

   public void initialize() {
/*  45 */     MapleData data = this.etcData.getData("CharacterCard.img");
/*  46 */     for (MapleData Card : data.getChildByPath("Card")) {
/*  47 */       int skillId = MapleDataTool.getIntConvert("skillID", Card, 0);
/*  48 */       if (skillId > 0) {
/*  49 */         this.cardEffects.put(Integer.valueOf(Integer.parseInt(Card.getName())), Integer.valueOf(skillId));
       }
     }
/*  52 */     for (MapleData Deck : data.getChildByPath("Deck")) {
/*  53 */       boolean uniqueEffect = (MapleDataTool.getIntConvert("uniqueEffect", Deck, 0) > 0);
/*  54 */       int skillId = MapleDataTool.getIntConvert("skillID", Deck, 0);
/*  55 */       if (uniqueEffect) {
/*  56 */         List<Integer> ids = new ArrayList<>();
/*  57 */         for (MapleData reqCardID : Deck.getChildByPath("reqCardID")) {
/*  58 */           ids.add(Integer.valueOf(MapleDataTool.getIntConvert(reqCardID)));
         }
/*  60 */         if (skillId > 0 && !ids.isEmpty()) {
/*  61 */           this.uniqueEffects.put(Integer.valueOf(skillId), ids);
         }
       }
     }
   }

   public Triple<Integer, Integer, Integer> getCardSkill(int job, int level) {
/*  68 */     int skillid = ((Integer)this.cardEffects.get(Integer.valueOf(job / 10))).intValue();
/*  69 */     if (skillid <= 0) {
/*  70 */       return null;
     }

/*  73 */     return new Triple(Integer.valueOf(skillid - 71000000), Integer.valueOf(skillid), Integer.valueOf(SkillConstants.getCardSkillLevel(level)));
   }

   public List<Integer> getUniqueSkills(List<Integer> special) {
/*  77 */     List<Integer> ret = new LinkedList<>();
/*  78 */     for (Map.Entry<Integer, List<Integer>> m : this.uniqueEffects.entrySet()) {
/*  79 */       if (((List)m.getValue()).contains(special.get(0)) && ((List)m.getValue()).contains(special.get(1)) && ((List)m.getValue()).contains(special.get(2))) {
/*  80 */         ret.add(m.getKey());
       }
     }
/*  83 */     return ret;
   }

   public boolean isUniqueEffects(int skillId) {
/*  87 */     return this.uniqueEffects.containsKey(Integer.valueOf(skillId));
   }

   public int getRankSkill(int level) {
/*  91 */     return SkillConstants.getCardSkillLevel(level) + 71001099;
   }

   public boolean canHaveCard(int level, int job) {
/*  95 */     return (level >= 30 && this.cardEffects.get(Integer.valueOf(job / 10)) != null);
   }

   public Map<Integer, CardData> loadCharacterCards(int accId, int serverId) {
/*  99 */     Map<Integer, CardData> cards = new LinkedHashMap<>();
/* 100 */     Map<Integer, Pair<Short, Short>> inf = loadCharactersInfo(accId, serverId);
/* 101 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 102 */         PreparedStatement ps = con.prepareStatement("SELECT * FROM `character_cards` WHERE `accid` = ?")) {
/* 103 */       ps.setInt(1, accId);
/* 104 */       try (ResultSet rs = ps.executeQuery()) {
/* 105 */         int deck1 = 0, deck2 = 3, deck3 = 6;
/* 106 */         while (rs.next()) {
/* 107 */           int chrId = rs.getInt("characterid");
/* 108 */           Pair<Short, Short> x = inf.get(Integer.valueOf(chrId));
/* 109 */           if (x == null) {
             continue;
           }

/* 113 */           if (!canHaveCard(((Short)x.getLeft()).shortValue(), ((Short)x.getRight()).shortValue())) {
             continue;
           }
/* 116 */           int position = rs.getInt("position");
/* 117 */           if (position < 4) {
/* 118 */             deck1++;
/* 119 */             cards.put(Integer.valueOf(deck1), new CardData(chrId, ((Short)x.getLeft()).shortValue(), ((Short)x.getRight()).shortValue())); continue;
           }
/* 121 */           if (position > 3 && position < 7) {
/* 122 */             deck2++;
/* 123 */             cards.put(Integer.valueOf(deck2), new CardData(chrId, ((Short)x.getLeft()).shortValue(), ((Short)x.getRight()).shortValue()));
             continue;
           }
/* 126 */           deck3++;
/* 127 */           cards.put(Integer.valueOf(deck3), new CardData(chrId, ((Short)x.getLeft()).shortValue(), ((Short)x.getRight()).shortValue()));
         }

       }

     }
/* 133 */     catch (SQLException e) {
/* 134 */       log.error("Failed to load character cards. Reason: ", e);
     }
/* 136 */     for (int i = 1; i <= 9; i++) {
/* 137 */       cards.computeIfAbsent(Integer.valueOf(i), k -> new CardData(0, 0, (short)0));
     }
/* 139 */     return cards;
   }

   public Map<Integer, Pair<Short, Short>> loadCharactersInfo(int accId, int serverId) {
/* 143 */     Map<Integer, Pair<Short, Short>> chars = new LinkedHashMap<>();
/* 144 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 145 */         PreparedStatement ps = con.prepareStatement("SELECT id, level, job FROM characters WHERE accountid = ? AND world = ?")) {
/* 146 */       ps.setInt(1, accId);
/* 147 */       ps.setInt(2, serverId);
/* 148 */       try (ResultSet rs = ps.executeQuery()) {
/* 149 */         while (rs.next()) {
/* 150 */           chars.put(Integer.valueOf(rs.getInt("id")), new Pair(Short.valueOf(rs.getShort("level")), Short.valueOf(rs.getShort("job"))));
         }
       }

/* 154 */     } catch (SQLException e) {
/* 155 */       System.err.println("error loading characters info. reason: " + e.toString());
     }
/* 157 */     return chars;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\CharacterCardFactory.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */